const form = document.querySelector('form')
const phone = document.querySelector('.phone')
const password = document.querySelector('.login_password')
const dis_box2 = document.querySelector('.dis_box2')
const error_con2 = document.querySelector('.error_con2')
const login_immediately = document.querySelector('.login_immediately')
const privacy = document.querySelector('.privacy')
form.addEventListener('submit', function (e) {

  e = e || window.event
  try { e.preventDefault() } catch (err) { e.returnValue === false }

  const phoneInp = phone.value
  const passwordInp = password.value
  if (phoneInp === '') {
    error_con2.innerText = '手机号或密码输入不能为空'
    dis_box2.style.display = 'block'
    return
  }
  if (passwordInp === '') {
    error_con2.innerText = '手机号或密码输入不能为空'
    dis_box2.style.display = 'block'
    return
  }
  if (privacy.checked === false) {
    error_con2.innerText = '请先阅读并接受隐私政策'
    dis_box2.style.display = 'block'
    return
  } else if (privacy.checked === true) {
    dis_box2.style.display = 'none'
  }
  let users = JSON.parse(window.localStorage.getItem('users')) || []
  console.log(users)
  let info = {
    id: !users.length ? 1 : users[users.length - 1].id + 1,
    phone: phoneInp,
    password: passwordInp
  }
  const res = users.find((item) => { return info.phone === item.phone })
  if (res) {
    error_con2.innerText = '用户名重复，请重新输入'
    dis_box2.style.display = 'block'
    return
  }
  users.push(info)
  window.localStorage.setItem('users', JSON.stringify(users))
  window.alert(`欢迎 ${ phoneInp } 加入我们`)
  window.location.href = './login.html'
})
phone.addEventListener('input', (e) => {
  e = e || window.event
  const phoneInp = phone.value
  if (phoneInp === '') {
    error_con2.innerText = '手机号或密码输入不能为空'
    dis_box2.style.display = 'block'
  } else if (/^1[3456789]\d{9}$/.test(phoneInp) === false) {
    error_con2.innerText = '您的手机号输入有误'
    dis_box2.style.display = 'block'
  } else {
    dis_box2.style.display = 'none'
  }
})
password.addEventListener('input', (e) => {
  e = e || window.event
  let passwordInp = password.value
  if (passwordInp === '') {
    error_con2.innerText = '密码输入不能为空'
    dis_box2.style.display = 'block'
  } else {
    testPass(passwordInp)
  }
  function testPass(passwordInp) {
    var rC = {
      lW: '[a-z]',
      uW: '[A-Z]',
      nW: '[0-9]',
      sW: '[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]'
    }
    function Reg(passwordInp, rStr) {
      var reg = new RegExp(rStr)
      if (reg.test(passwordInp)) return true
      else return false
    }
    if (passwordInp.length < 6) {
      error_con2.innerText = '您的密码长度太短'
      dis_box2.style.display = 'block'
      return false
    } else {
      var tR = {
        l: Reg(passwordInp, rC.lW),
        u: Reg(passwordInp, rC.uW),
        n: Reg(passwordInp, rC.nW),
        s: Reg(passwordInp, rC.sW)
      }
      if ((tR.l && tR.u && tR.n) || (tR.l && tR.u && tR.s) || (tR.s && tR.u && tR.n) || (tR.s && tR.l && tR.n)) {

        dis_box2.style.display = 'none'
        return true;
      } else {
        error_con2.innerText = '您的密码必须含有“小写字母”、“大写字母”、“数字”、“特殊符号”中的任意三种'
        dis_box2.style.display = 'block'
        return false
      }
    }
  }
})


